package com.wanbangee.marksystem.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo;
import com.wanbangee.marksystem.entities.StatisticsScoreTarget;
import com.wanbangee.marksystem.entities.StatisticsSubReport;
import com.wanbangee.marksystem.service.ScoreAndAnswerSheetService;
import com.wanbangee.marksystem.service.SettingClientService;

/**
 * 成绩与答案的控制层
 * @author yongcode
 *
 */
@RestController
public class ScoreAndAnswerSheetController {

	@Autowired
	private ScoreAndAnswerSheetService scoreAndAnswerSheetService;   //注入成绩与答案业务对象
	
	@Autowired 
	private SettingClientService SettingClientService;   //注入考试管理服务的对外接口
	
	/**
	 * 根据考试ID和科目ID查询分数段指标表
	 * @param subId
	 * @param examId
	 * @return
	 */
	@GetMapping("/scoreAndAnswerSheet/selectScoreSubsection")
	public Map<String, Object> selectScoreSubsection(Integer subId ,Integer examId){
		//用来存储提示信息，和状态码，数据
		Map<String, Object> responseMap = new HashMap<String, Object>();
		
		//调用scoreAndAnswerSheetService中selectScoreSubsectionByExamIdAndSubId方法查询分数段指标表
		List<StatisticsScoreTarget> ssts = scoreAndAnswerSheetService.selectScoreSubsectionByExamIdAndSubId(examId, subId);
		
		if(ssts == null || ssts.size() == 0) {  //查询失败
			responseMap.put("code", 10013);
			responseMap.put("msg", "未查询到该科目的分数段指标表");
		}else {
			//将查询到的科目信息放入map中返回给前端
			responseMap.put("ssts", ssts);
			//返回状态码1001，提示信息：接口成功
			responseMap.put("code", 1001);
			responseMap.put("msg", "接口成功");
		}
		return responseMap;
	}
	
	/**
	 * 保存分数段
	 * @param examId 考试Id
	 * @param subId	科目Id
	 * @param subNm	科目名称
	 * @param subsection	分数段信息表
	 * @return
	 */
	@PostMapping("/scoreAndAnswerSheet/saveScoreSubsection")
	public Map<String, Object> saveScoreSubsection(@RequestBody JSONArray jsonArray){
		return scoreAndAnswerSheetService.insertStatisticsScoreTarget(jsonArray);
	}
	
	/**
	 * 分页查询学生得分与答卷
	 * @param examId
	 * @param claId
	 * @param subId
	 * @param stuNmOrStuNo
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@GetMapping("/scoreAndAnswerSheet/selectStuScoreAndAnswerSheet")
	public Map<String, Object> selectStuScoreAndAnswerSheet(Integer examId,Integer claId,Integer subId,String stuNmOrStuNo,@RequestParam(value = "pageNum",defaultValue="1")Integer pageNum,@RequestParam(value = "pageSize",defaultValue="10")Integer pageSize){
		//用来存储提示信息，和状态码，数据
		Map<String, Object> responseMap = new HashMap<String, Object>();
		
		//调用scoreAndAnswerSheetService中selectScoreSubsectionByExamIdAndSubId方法查询分数段指标表
		List<StatisticsSubReport> ssrs = scoreAndAnswerSheetService.selectStuScoreAndAnswerSheet(examId, claId, subId, stuNmOrStuNo,pageNum,pageSize);
		
		//將查詢得到的學生考試信息放入分頁工具中
		 PageInfo pageInfo = new PageInfo(ssrs);
		 
		if(ssrs == null || ssrs.size() == 0) {  //查询失败
			responseMap.put("code", 10013);
			responseMap.put("msg", "未查询到该科目班级学生考试信息");
		}else {
			//将查询到的科目信息放入map中返回给前端
			responseMap.put("pageInfo", pageInfo);
			//返回状态码1001，提示信息：接口成功
			responseMap.put("code", 1001);
			responseMap.put("msg", "接口成功");
		}
		return responseMap;
	}
	
	/**
	 * 查询学生对应的试卷url
	 * @param examId
	 * @param stuId
	 * @param subId
	 * @return
	 */
	@GetMapping("/scoreAndAnswerSheet/selectStuExamaUrl")
	public Map<String, Object> selectStuExamaSheetUrl(Integer examId,Integer stuId,Integer subId){
		return SettingClientService.selectStuExamaSheetUrl(examId, stuId, subId);
	}
	
}
